package cn.jingzhuan.element.demo.view.util.audio

import android.app.PendingIntent
import android.content.ComponentName
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.Drawable


/**
 * Created by KNOX.
 */
class AudioNotification(private val service : AudioService) : AudioNotificationParent(service) {

  override fun update() {
    val audio : Audio? = AudioPlayerManager.currentAudio()
    if (audio != null) {
//      logi("AudioNotification", "audio id > " + audio.id.toString())
//      val activityIntent = Intent(service, ArticleDetailActivity::class.java)
//      activityIntent.putExtra(ArticleDetailActivity.ID, audio.id)
//      val contentIntent = PendingIntent.getActivity(service, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_ONE_SHOT)
//      val deleteIntent = PendingIntent.getBroadcast(service, 0, Intent(service, NotificationCanceledReceiver::class.java), 0)
//      val icon : Int = if (service.isPlaying()) {
//        R.drawable.ic_audio_pause
//      } else {
//        R.drawable.ic_audio_play
//      }
//      val playPauseAction = NotificationCompat.Action(icon, "", playbackAction(ACTION_PLAY_PAUSE))
//
//      val notification = NotificationCompat.Builder(service, NOTIFICATION_CHANNEL_ID)
//          .setShowWhen(false)
//          .setSmallIcon(R.drawable.ic_notification_small)
//          .setLargeIcon(createBitmap(
//              compatDrawable(service, R.drawable.ic_notification_image_big) !!, 1f))
//          .setContentTitle(audio.title)
//          .setContentText(audio.author)
//          .setContentIntent(contentIntent)
//          .setDeleteIntent(deleteIntent)
//          .setCategory(NotificationCompat.CATEGORY_SERVICE)
//          .setPriority(NotificationCompat.PRIORITY_HIGH)
//          .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
//          .setOngoing(service.isPlaying())
//          .addAction(playPauseAction)
//          .setColor(compatColor(service, R.color.colorAccent))
//          .setColorized(true)
//          .setStyle(android.support.v4.media.app.NotificationCompat.MediaStyle()
//              .setMediaSession(service.getMediaSession()?.sessionToken))
//          .build()

//      postNotification(notification)
    } else {
      stop()
    }
  }

  private fun playbackAction(action : String) : PendingIntent {
    val serviceName = ComponentName(service, AudioService::class.java)
    val intent = Intent(action)
    intent.component = serviceName
    return PendingIntent.getService(service, 0, intent, 0)
  }

  private fun createBitmap(drawable : Drawable, sizeMultiplier : Float) : Bitmap {
    val bitmap = Bitmap.createBitmap((drawable.intrinsicWidth * sizeMultiplier).toInt(),
        (drawable.intrinsicHeight * sizeMultiplier).toInt(), Bitmap.Config.ARGB_8888)
    val c = Canvas(bitmap)
    drawable.setBounds(0, 0, c.width, c.height)
    drawable.draw(c)
    return bitmap
  }
}